using System;
using System.Reflection;
using System.Web;
using System.Text;
using System.IO;
//using log4net;


namespace PHSRAG.Insight21
{	
	/// <summary>
	/// Summary description for CustomHTTPModule.
	/// </summary>
	public class SessionLoggerModule : IHttpModule		  
	{

		//private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

		
		public SessionLoggerModule()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		
		// Classes that inherit IHttpModule 
		// must implement the Init and Dispose methods.
		public void Init(HttpApplication app)
		{


			app.AcquireRequestState +=new EventHandler(app_AcquireRequestState);
			
	

			//app.PostRequestHandlerExecute +=new EventHandler(app_PostRequestHandlerExecute);
	
		}

		/// <summary>
		/// 
		/// </summary>
		public void Dispose()
		{
			// Add code to clean up the
			// instance variables of a module.

		}

	

		private void app_AcquireRequestState(object sender, EventArgs e)
		{

			StringBuilder sb = new StringBuilder();
			HttpApplication httpApp = (HttpApplication)sender;
			HttpContext ctx = HttpContext.Current;

			sb.Append("Page " + ctx.Request.Url.PathAndQuery + "\n");

			
			
			
			if (ctx.Session.Keys.Count > 0)
			{
				sb.Append("Total Objects in Session = " + ctx.Session.Keys.Count.ToString() + "\n");
			}
			
			foreach (string key in ctx.Session.Keys)
			{
			
				sb.Append("Object Type = ");
				sb.Append(ctx.Session[key].GetType().ToString()+ "\n");
				sb.Append("Object Name = ");
				sb.Append(ctx.Session[key].ToString() + "\n");
			}

			
			long startMemory = System.GC.GetTotalMemory(false);
			long memoryInMb = (startMemory / 1024) / 1024 ;
			
			sb.Append("Total Memory Consumed = ");
			sb.Append(memoryInMb.ToString() + " mb");

			//log.Info(sb.ToString());

		

		}
	}
}
